package com.lepu.app.fun.chat.business.uiflow;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by chuanyhu on 2014/8/9.
 */
public class UIComDesc {

    /**
     * 解析单个节点 中的属性
     * 节点中的子节点
     */
    public static int UIComType_Activity = 1;
    public static int UIComType_Fragment = 2;

    private Map<String, GoAction> goActionMap = new HashMap<String, GoAction>();

    private String name;

    private int type; //跳转类型

    private Map<String, String> attrMap; //(name ,attr)

    public void init(Element comEle, String defPackage) {
        String attrname = comEle.getAttribute("name");
        this.name = attrname.startsWith(".") ? defPackage + attrname : attrname;
        if (name.indexOf("Activity") >= 0) {
            this.type = UIComType_Activity;
        } else if (name.indexOf("Fragment") >= 0) {
            this.type = UIComType_Fragment;
        } else {
            throw new RuntimeException("UIComponent :" + name + " not contain Activity Or Fragment");
        }
        NamedNodeMap namedNodeMap = comEle.getAttributes();
        attrMap = new HashMap();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            Node node = namedNodeMap.item(i);
            String name = node.getNodeName();
            String value = node.getNodeValue();
            if (value != null && value.length() > 0) {
                attrMap.put(name, value);
            }
        }
    }

    public int getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public GoAction getGoAction(String name) {
        return this.goActionMap.get(name);
    }

    public void addGoAction(String goActionName, GoAction action) {
        goActionMap.put(goActionName, action);
    }

    public String getAttribute(String name) {
        return attrMap.get(name);
    }
}
